# -*- coding: utf-8 -*-
__author__ = 'wwj'
import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
from celery import Celery
import sys
import os
from celery.utils.log import get_task_logger
import pymongo
from datetime import *
# app = Celery('dispatch',include=['tasks.xvideo'])
import config
app = Celery('dispatch')
app.config_from_object(config)
import hashlib
import requests
from subprocess import call
import re
import redis
logger = get_task_logger(__name__)
pool = redis.ConnectionPool(host='localhost', port=6379)
redisconn = redis.Redis(connection_pool=pool)
import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.connect(
        "47.75.39.50",
        "xiaolei",
        "ken402402",
        "shantianjunbak",charset="utf8mb4",cursorclass=MySQLdb.cursors.DictCursor,
        connect_timeout=100000000
        )

def getdb(sql):
    cursor = conn.cursor()
    cursor.execute(sql)
    tmp = cursor.fetchallDict()
    return tmp


def prepare(self):
    if not redisconn.exists("city:cateList"):
        sql = "select name from mode union select name from cate"
        cateLsit = getdb(sql)
        cateLsit = "|".join([x["name"] for x in cateLsit])
        redisconn.set("city:cateLsit",cateLsit)
        
        tmp = getdb("select id,name from cate")
        cate = {x["name"]:x["id"] for x in tmp}
        redisconn.hmset("city:cate",cate)
        
        tmp = getdb("select id,name from mode")
        mode = {x["name"]:x["id"] for x in tmp}
        redisconn.hmset("city:mode",mode)

        tmp = getdb("select id,name,level from area")
        area = {x["name"]+x["level"]:x["id"] for x in tmp}
        redisconn.hmset("city:area",area)

        # tmp = self.getdb("select distinct(url),id from store")
        # for x in tmp:
        #     # url = hashlib.md5(x["url"].encode("utf8")).hexdigest()
        #     url = x["url"]
        #     redisconn.hset("stores",url,x["id"])
        #
        # tmp = self.getdb("select url,id,sid from girl")
        # for x in tmp:
        #     redisconn.hset("girl",x["url"],(x["id"],x["sid"]))


@app.task(bind=True,queue="city_clean",max_retries=1,ignore_result=True)
def run(self,y):
    prepare()
    data["url"] = y["url"].replace("https://www.cityheaven.net","")
    if y.get("workTime"):
        data["workTime"] = "".join(y["workTime"])
    else:
        data["workTime"] = ""
    data["name"] = y.get("name")
    data["type"] = y.get("type")
    data["cover"] = ",".join(y.get("cover",[]))
    if y.get("cover"):
        data["thumb"] = y.get("cover")[0]
    if y.get("smallcover"):
        data["scover"] = re.sub("\?cache.*", "", y.get("smallcover",""))
    # tmp = map(lambda url: re.sub("\?cache.*", "", url), tmp)
    data["email"] = self.emails.get(y["url"],"")
    if y.get("address"):
        data["address"] = re.sub("\[.+\]","",y.get("address",""))
        xx = "派遣エリア：".decode("utf")
        data["address"] = data["address"].replace(xx,"")
        data["address"] = "" if len(data["address"]) > 200 else data["address"]
    else:
        data["address"] = ""
    if y.get("phone"):
        data["phone"] = y.get("phone").strip()
    if y.get("site"):
        data["site"] = ",".join(y["site"]) if type(y["site"]) == list else y["site"]
    else:
        data["site"] = ""
    data["goto"] = "" if type(y.get("goto")) == list else y.get("goto","") or ""
    data["info"] = y.get("info","")
    if not y.get("cate2") and not y.get("cate1"):
        data["cate"] = "dispatch"
        data["mode"] = "dispatch"
    else:
        data["cate"] = y["cate2"]
        data["mode"] = y["cate1"]
    if not y.get("cate1") and y.get("cate2","").find("(") > 0:
        if self.reg.search(y["cate2"]):
            data["mode"] = self.reg.search(y["cate2"]).groups()[0]
            data["cate"] = self.reg.search(y["cate2"]).groups()[1]
    data["price"] = y.get("price") or ""
    if y["price"]:
        try:
            prix = re.search(r'(\d+)\D+(\d+)',y['price']).groups()
            data['prix'] = str(int(prix[1])/int(prix[0]) * 60)
        except:
            data['prix'] = ""
    data["urlHash"] = str(crc32(data["url"])).replace("-","")
    for yy in self.cate:
        if yy["name"] == data["cate"]:
            data["cateId"] = yy["id"]
    for yy in self.mode:
        if yy["name"] == data["mode"]:
            data["modeId"] = yy["id"]
    if y["area"]:
        for yy in self.area:
            if yy["name"] == re.sub(self.cateLsit,"",y["area"][1]) and int(yy["level"]) == 1:
                data['area'] = yy["id"]
            elif yy["name"] == re.sub(self.cateLsit,"",y["area"][3]) and int(yy["level"]) == 2:
                data['city'] = yy["id"]
            elif yy["name"] == re.sub(self.cateLsit,"",y["area"][4]) and int(yy["level"]) == 3:
                data['town'] = yy["id"]
    return buildsql(data)


def buildsql(data):
    data_values = "(" + "%s," * (len(data.keys())) + ")"
    data_values = data_values.replace(',)', ')')
    keys = str(tuple(keys)).replace("'",'')
    values = [data]
    for value in values:
        dd = []
        sql = "insert ignore into %s %s values %s" % (self.mysqlTable,keys,data_values)
        # sql = "replace into %s %s values %s" % (self.mysqlTable,keys,data_values)
        # sql = "INSERT INTO auto %s(%s) VALUES (%s) ON DUPLICATE KEY UPDATE urlHash=VALUES(urlHash)"
        for v in value:
            dd.append([MySQLdb.escape_string(str(y).strip().replace("%","")) for yy,y in v.iteritems()])
        try:
            pp(dd)
            self.conn.ping(True)
            self.cursor = self.conn.cursor()
            self.cursor.executemany(sql,dd)
            self.conn.commit()
            del self.datas[:self.exesize]
        except Exception as ex:
            # import ipdb;ipdb.set_trace()
            self.conn.ping(True)
            print ex

